package com.easy.controller;

import com.easy.bean.Order;
import com.easy.common.CommonResult;
import com.easy.dao.IInInventoryDao;
import com.easy.service.IInInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ininventory")
public class InInventoryController {
    @Autowired
    IInInventoryService is;
    @Autowired
    IInInventoryDao dao;

    @GetMapping("/all")
    public CommonResult getIInInventory() {
        List<Order> list = is.getInInventory();
        return CommonResult.success(list);
    }

    @GetMapping("/{in_id}")
    public CommonResult getInInventoryByInId(@PathVariable String in_id) {
        List<Order> orders = dao.getInInventoryByInId(in_id);
        return CommonResult.success(orders);
    }

    @PostMapping("/add")
    public CommonResult addInInventory(@RequestBody Order order){
        int count = is.addInInventory(order);
        if(count>0) {
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }    }

    @PutMapping("/edit/{id}")
    public CommonResult editInInventory(@PathVariable int id, @RequestBody Order order){
        order.setId(id);  // 将URL中的id设置到User对象中
        int count = is.editInInventory(order);
        if(count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.fail();
        }
    }

    @DeleteMapping("/del/{id}")
    public CommonResult delInInventory(@PathVariable int id) {
        int count = is.delInInventory(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.fail();
        }
    }

    //  查询商品名或id
    @GetMapping("/search")
    public CommonResult searchInInventory(@RequestParam String keyword){
        List<Order> inventories = is.searchInInventory(keyword);
        return CommonResult.success(inventories);
    }

    //   分页
    @GetMapping("/paged")
    public CommonResult getPagedInInventory(@RequestParam int page, @RequestParam int size) {
        List<Order> list = is.getPagedInInventory(page, size);
        int total = is.getTotalInInventoryCount();
        return CommonResult.success(list, total);
    }
}
